package com.frank.demo5;

import okhttp3.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import okhttp3.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {

    private static final Logger log = LoggerFactory.getLogger(IndexController.class);

    @RequestMapping("/{index}")
    public String index(){
        return "index";
    }

    @ResponseBody
    @PostMapping("/comment")
    public String Comment(@RequestBody Comment comment){
        System.out.println(comment.toString());
        try {
            sendComment(comment);
        } catch (IOException e) {
            log.error("任务失败:{}", comment.getUrl());
            throw new RuntimeException(e);
        }
        return "任务执行成功";
    }


    private void sendComment(Comment comment) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
        okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, String.format("commentId=&content=%s&articleId=134923922",comment.getComment()));
        Request request = new Request.Builder()
                .url("https://blog.csdn.net/phoenix/web/v1/comment/submit")
                .method("POST", body)
                .addHeader("Cookie", "uuid_tt_dd=10_37231461370-1697084436486-570136; UN=weixin_38331049; b-user-id=10c06791-ccc0-2b57-d82f-0817b769b2a5; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_37231461370-1697084436486-570136!5744*1*weixin_38331049; pluginId=kfkdboecolemdjodhmhmcibjocfopejo; pluginVersion=3.0.6; pluginUUID=10_32002310846-1645801447804-932029; UserName=weixin_38331049; UserInfo=073c716ec48f43d9a6f32837e4a10440; UserToken=073c716ec48f43d9a6f32837e4a10440; UserNick=%E7%8E%B2%E7%8F%91%C2%B7; AU=F09; BT=1713116917493; p_uid=U010000; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22weixin_38331049%22%2C%22scope%22%3A1%7D%7D; c_ins_prid=-; c_ins_rid=1716710024186_450134; c_ins_fref=-; c_ins_fpage=/index.html; c_ins_um=-; ins_first_time=1716710024637; cf_clearance=beoyaXi_4aJ19wohfF1Qvvg2HmEcVHuIR407IdjKAIE-1716732583-1.0.1.1-AVp3LwdAIx_jcchzFTBuMF5jo1Vvj8NIUlkNXF7PoJnD0zBs6Qv.4NE89Ka57wuR9Z7MSnlLvegTnMF.Wjgo2Q; Hm_lvt_ec8a58cd84a81850bcbd95ef89524721=1720790340; dc_sid=27ae52a94b3662a22e6439770d13e7b9; https_waf_cookie=37dbab75-06a8-4ec7a00e273d7073b07e2321e77a7af528fc; c_segment=8; c_adb=1; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1720789102,1720866411,1720882736,1721827804; HMACCOUNT=3344A7DD3C6689D8; c_hasSub=true; _clck=1jcs9so%7C2%7Cfnq%7C0%7C1565; _gid=GA1.2.1715245913.1721827808; creative_btn_mp=3; SidecHatdocDescBoxNum=true; dc_session_id=10_1721840732019.926503; c_pref=default; _ga=GA1.1.1322114661.1697388140; c_first_ref=default; creativeSetApiNew=%7B%22toolbarImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20230921102607.png%22%2C%22publishSuccessImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20240229024608.png%22%2C%22articleNum%22%3A431%2C%22type%22%3A2%2C%22oldUser%22%3Atrue%2C%22useSeven%22%3Afalse%2C%22oldFullVersion%22%3Atrue%2C%22userName%22%3A%22weixin_38331049%22%7D; _ga_7W1N0GEY1P=GS1.1.1721840828.18.1.1721845301.60.0.0; waf_captcha_marker=120fb7f80bbda5867d1b684cf33909464b0ee97786d8d4edaa5e697c8a25fd8c; dc_tos=sh54h0; c_ref=default; c_first_page=https%3A//koala.blog.csdn.net/article/details/134923922; c_dsid=11_1721845477923.802894; c_page_id=default; log_Id_pv=382; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1721845478; _clsk=1uuxy5k%7C1721845478768%7C30%7C0%7Cm.clarity.ms%2Fcollect; log_Id_view=12420; log_Id_click=263")
                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .build();
        Response response = client.newCall(request).execute();
        // 处理响应
        if (response.isSuccessful()) {
            System.out.println("Response Code: " + response.code());
            System.out.println("Response Body: " + response.body().string());
        } else {
            System.out.println("Request failed with code: " + response.code());
        }
    }
}
